package com.gzhryc.socket.message;

import java.nio.charset.Charset;
import java.util.HashMap;

import org.eclipse.paho.client.mqttv3.MqttMessage;

import com.gzhryc.socket.SocketConstants;

public class MqttSocketMessage extends ByteSocketMessage{

	MqttMessage mMqttMsg;
	
	public MqttSocketMessage(String topic,Integer qos,byte[] bytes){
		super(bytes);
		this.attributes = new HashMap<String,Object>();
		this.attributes.put(SocketConstants.ATTR_MQTT_TOPIC, topic);
		this.attributes.put(SocketConstants.ATTR_MQTT_QOS, qos);
	}

	public MqttSocketMessage(String topic,Integer qos,String content){
		super(content);
		this.attributes = new HashMap<String,Object>();
		this.attributes.put(SocketConstants.ATTR_MQTT_TOPIC, topic);
		this.attributes.put(SocketConstants.ATTR_MQTT_QOS, qos);
		this.bytes = content.getBytes(Charset.forName("UTF-8"));
	}
	
	public MqttSocketMessage(String topic,MqttMessage message) {
		super(message.getPayload());
		this.attributes = new HashMap<String,Object>();
		this.attributes.put(SocketConstants.ATTR_MQTT_TOPIC, topic);
		this.attributes.put(SocketConstants.ATTR_MQTT_QOS, message.getQos());
		this.mMqttMsg = message;
	}
	
	public String getTopic() {
		Object obj = this.attributes.get(SocketConstants.ATTR_MQTT_TOPIC);
		if(obj != null) {
			return obj.toString();
		}
		return null;
	}
	
	public Integer getQos() {
		Object obj = this.attributes.get(SocketConstants.ATTR_MQTT_QOS);
		if(obj != null) {
			return Integer.valueOf(obj.toString());
		}
		return null;
	}
}
